package com.itita.ww2.model.item.prop;

import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.data.MissionConfig;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.game.user.UserInventoryManager;
import com.itita.ww2.game.user.UserResource;
import com.itita.ww2.model.CodeType;
import com.itita.ww2.model.item.enums.param1.P1FunctionType;
import com.itita.ww2.model.item.interfaces.IUseable;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.MathUtil;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSDataType;
import com.smartfoxserver.v2.entities.data.SFSDataWrapper;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class FunctionPropStrategy implements IUseable {
	private ItemProp itemProp;

	public FunctionPropStrategy(ItemProp itemProp) {
		this.itemProp = itemProp;
	}

	@Override
	public ISFSObject use(UserProfile userProfile, ISFSObject params) throws WW2Exception {
		ISFSObject ref = SFSObject.newInstance();
		P1FunctionType type = P1FunctionType.values()[itemProp.getParam1()];
		switch (type) {
		case RUSH_COIN:
			ref = rushCoin(userProfile, params.getUtfString("missionId"));
			break;
		default:
			throw new WW2Exception(WW2ExceptionCode.INVALID_OPT);
		}
		return ref;
	}

	private ISFSObject rushCoin(UserProfile userProfile, String missionId) throws WW2Exception {
		// 体力不够 不给使用
		if (!userProfile.getUserInfo().isFatigueEnough(missionId)) {
			throw new WW2Exception(WW2ExceptionCode.USER_FATIGUE_NOT_ENOUGH);
		}
		ISFSObject ref = SFSObject.newInstance();
		// 资源奖励
		UserResource resManager = userProfile.getResManager();
		ISFSObject rewardObj = MissionConfig.getRewardComplete(missionId);
		ISFSObject resObj = SFSObject.newInstance();
		for (String key : rewardObj.getKeys()) {
			CodeType type = CodeType.codeValueOf(key);
			if (type == CodeType.RESOURCE) {
				resManager.operResWithCheck(key, rewardObj.getInt(key));
				resObj.putInt(key, rewardObj.getInt(key));
			}
		}
		resManager.update();
		resManager.sync();
		ref.putSFSObject("res", resObj);

		// 道具奖励
		UserInventoryManager inventoryManager = userProfile.getInventory();
		ISFSObject itemObj = SFSObject.newInstance();
		// 叠加
		ISFSObject rushRewardObj = MissionConfig.getRewardRush(missionId);
		for (String key : rushRewardObj.getKeys()) {
			inventoryManager.addItem(key, rushRewardObj.getInt(key));
			itemObj.putInt(key, rushRewardObj.getInt(key));
		}
		// 正常
		ISFSObject completeRewardObj = MissionConfig.getRewardComplete(missionId);
		for (String key : completeRewardObj.getKeys()) {
			CodeType codeType = CodeType.codeValueOf(key);
			if (codeType != CodeType.ITEM) {
				continue;
			}
			int count = 0;
			SFSDataWrapper item = completeRewardObj.get(key);
			if (item.getTypeId() == SFSDataType.SFS_OBJECT) {
				// 数量随机
				ISFSObject obj = (ISFSObject) item.getObject();
				int min = obj.getInt("min");
				int max = obj.getInt("max");
				count = MathUtil.RandomInt(min, max);
			} else if (item.getTypeId() == SFSDataType.INT) {
				count = (int) item.getObject();
			}
			// 数量0个就不加了哈哈
			if (count == 0) {
				continue;
			}
			inventoryManager.addItem(key, count);
			itemObj.putInt(key, count);
		}
		// 扣除体力
		userProfile.getUserInfo().fatigueCostByMission(missionId);
		// 指挥官经验+
		userProfile.getUserInfo().growExpByMission(missionId);
		ref.putSFSObject("item", itemObj);
		return ref;
	}

}
